package com.youi.web;

import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.User;
import com.youi.entity.UserSearchHistory;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.service.UserSearchHistoryService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @Author: winty
 * @Date: 2020/5/14 9:57
 */
@RestController
public class UserSearchHistoryController extends HttpRequestManage {

    @Autowired
    private UserSearchHistoryService userSearchHistoryService;

    /**
     * 添加搜索关键字
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/userGetInfo/addSearchKeyword")
    public String addSearchKeyword(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        String keyword = optString("keyword", request);
        if (StringUtils.isBlank(keyword)) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "关键字不能为空");
        }
        keyword = StringUtils.trim(keyword);
        if (keyword.length() > 30) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "关键字最大长度30");
        }
        User user = (User) HttpSessionManage.getAttribute(request, SessionKey.USER);

        UserSearchHistory userSearchHistory = new UserSearchHistory();
        userSearchHistory.setKeyword(keyword);
        userSearchHistory.setUserId(user.getId());
        userSearchHistory.setDelStatus(DelStatus.NODEL);
        userSearchHistory.setGmtCreated(new Date());
        userSearchHistory.setGmtModify(new Date());
        UserSearchHistory resultMap = userSearchHistoryService.addSearchKeyword(userSearchHistory);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", resultMap);
    }

    /**
     * 获取平台热搜关键字（频率最高）
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/listHotKeyword")
    public String listHotKeyword(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        //平台热搜关键字展示个数，默认为6
        Integer limit = optInteger("limit", request, 6);

        List<UserSearchHistory> keywordList = userSearchHistoryService.listHotKeyword(new HashMap<String, Object>(1) {{
            put("limit", 100);
            put("topLimit", limit);
        }});
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", keywordList);
    }

    /**
     * 获取用户搜索关键字（最新）
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/userGetInfo/listUserKeyword")
    public String listUserKeyword(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        User user = (User) HttpSessionManage.getAttribute(request, SessionKey.USER);
        Integer limit = optInteger("limit", request, 5);

        List<UserSearchHistory> keywordList = userSearchHistoryService.listUserKeyword(new HashMap<String, Object>(2) {{
            put("userId", user.getId());
            put("limit", limit);
        }});
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", keywordList);
    }

    /**
     * 删除搜索关键字
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/userGetInfo/deleteKeywordById")
    public String deleteKeywordById(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        User user = (User) HttpSessionManage.getAttribute(request, SessionKey.USER);
        Long userId = user.getId();
        Long id = optLong("id", request);
        userSearchHistoryService.deleteKeywordById(userId, id);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }


    /**
     * 删除用户所有搜索记录
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/userGetInfo/deleteUserAllKeyword")
    public String deleteUserAllKeyword(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        User user = (User) HttpSessionManage.getAttribute(request, SessionKey.USER);
        Long userId = user.getId();
        userSearchHistoryService.deleteKeywordByUserId(userId);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }


}